﻿#include "zbase64.h"
#include <QDebug>
#include "msgbox.h"

using namespace std;

ZBase64::ZBase64()
{
}

string ZBase64::Encode(const unsigned char* Data,int DataByte)
{
    //编码表
    const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    //返回值
    string strEncode;
    unsigned char Tmp[4]={0};
    int LineLength=0;
    for(int i=0;i<(int)(DataByte / 3);i++)
    {
        Tmp[1] = *Data++;
        Tmp[2] = *Data++;
        Tmp[3] = *Data++;
        strEncode+= EncodeTable[Tmp[1] >> 2];
        strEncode+= EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];
        strEncode+= EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];
        strEncode+= EncodeTable[Tmp[3] & 0x3F];
        if(LineLength+=4,LineLength==76) {strEncode+="\r\n";LineLength=0;}
    }
    //对剩余数据进行编码
    int Mod=DataByte % 3;
    if(Mod==1)
    {
        Tmp[1] = *Data++;
        strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
        strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4)];
        strEncode+= "==";
    }
    else if(Mod==2)
    {
        Tmp[1] = *Data++;
        Tmp[2] = *Data++;
        strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
        strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];
        strEncode+= EncodeTable[((Tmp[2] & 0x0F) << 2)];
        strEncode+= "=";
    }

    return strEncode;
}

string ZBase64::Decode(const char* Data,int DataByte)
{
    //解码表
    const char DecodeTable[] =
    {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        62, // '+'
        0, 0, 0,
        63, // '/'
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
        0, 0, 0, 0, 0, 0, 0,
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
        13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
        0, 0, 0, 0, 0, 0,
        26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
        39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
    };
    //返回值
    int OutByte = 0;

    string strDecode;
    int nValue;
    int i= 0;
    while (i < DataByte)
    {
        if (*Data != '\r' && *Data!='\n')
        {
            nValue = DecodeTable[*Data++] << 18;
            nValue += DecodeTable[*Data++] << 12;
            strDecode+=(nValue & 0x00FF0000) >> 16;
            OutByte++;
            if (*Data != '=')
            {
                nValue += DecodeTable[*Data++] << 6;
                strDecode+=(nValue & 0x0000FF00) >> 8;
                OutByte++;
                if (*Data != '=')
                {
                    nValue += DecodeTable[*Data++];
                    strDecode+=nValue & 0x000000FF;
                    OutByte++;
                }
            }
            i += 4;
        }
        else// 回车换行,跳过
        {
            Data++;
            i++;
        }
     }
    return strDecode;
}

int ZBase64::DecodeBarCode(QString Base64Str,QString &tb_EB, QString &tb_XP , QString &cb_Number,QString &tb_Code,QString &dt_PDate,QString &tb_validity,QString &tb_serial_code)
{

    string Code = "";
#if 0
    //int OutByte = -1;
    QByteArray ba2;
    ba2.append(Base64Str);     //也可以 ba2 44444= s2.toLatin1();
    const char *c2 = ba2.data();
    int Base64StrLength = Base64Str.length();


    Code = Decode(c2,Base64StrLength);
    QString QCode = QString::fromStdString(Code);
#else
    QString QCode = Base64Str;
#endif
    //copy
    //位号	1	2	3	4	5	6	7	8	9	10	11	12	13	14	15	16	17	18	19	20
    //编码	E	B	X	P	M	1	0	1	2	1	1	1	0	6	3	6	0	0	0	1
    //      A	    B	    C	    D	    E	                    F       G

    //A：公司代码EB：Etta Biotech
//    B：产品代码XP：X-Porator细胞电转染仪
//    C：产品序列，H：HiDEN高密度矩阵电转染仪；F：FLow流式电转染仪；M：免疫细胞专用电转染仪
//    D：耗材代码，0-99
//    E：生产（灭菌）日期（xx年xx月xx日）
//    F：有效期，xx个月
//    G：流水码，0-9999
    if (QCode.length() == 20)
    {
        tb_EB = QCode.mid(0,2);
        tb_XP = QCode.mid(2,2);
        cb_Number = QCode.mid(4,2);
        tb_Code = QCode.mid(6,2);
        dt_PDate = QCode.mid(8,6);
        tb_validity = QCode.mid(14,2);
        tb_serial_code = QCode.mid(16,4);

        if(cb_Number.contains("H",Qt::CaseSensitive))
        {
            cb_Number = "HiDEN高密度矩阵电转染仪";
        }
        else if(cb_Number.contains("M",Qt::CaseSensitive))
        {
            cb_Number = "FLow流式电转染仪";
        }
        else if(cb_Number.contains("F",Qt::CaseSensitive))
        {
            cb_Number = "免疫细胞专用电转染仪";
        }


        if (!tb_EB.contains("EB",Qt::CaseSensitive) && !tb_XP.contains("XP",Qt::CaseSensitive))
        {
            return -3;
        }

        qDebug() << "Code!=   "  << QCode  << endl;
    }
    else if(QCode.length() == 0)
    {
//        MsgBox::information(this, QString("Error"),QString("Consumables cannot be empty!"));
//            MsgBox::information(this, QString(tr("Error")),QString(tr("Consumables cannot be empty!")));
        return -1;
    }
    else
    {
       qDebug(   ) << "error Code " << QCode << endl;
         return -2;

    }

    return 0;


}
